using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using ca.metaobjects.csogateway;
using ca.metaobjects.csogateway.Converters.Interfaces;

namespace ca.metaobjects.csogateway.Converters.System
{
	/// <summary>
	/// <para>
	/// Provides methods to convert instances of System.TimeSpan into an instance of CsoGateway.System.TimeSpan
	/// </para>
	/// 
	/// <para>
	/// Converter for
	/// <list type="bullet">
	/// <item>System.TimeSpan</item>
	/// </list>
	/// </para>
	///
	/// <para>
	/// Converts to
	/// <list type="bullet">
	/// <item>CsoGateway.System.TimeSpan</item>
	/// </list>
	///	</para>
	/// 
	/// </summary>
	public class TimeSpanToClientSideConverter : IToClientSideConverter
	{
		/// <summary>
		/// Generates a string of javascript code that, when evaluated, creates an instance of CsoGateway.System.TimeSpan
		/// that is the client side equivalent of the given object.
		/// The generated string is appended in the given StringBuffer. 
		/// </summary>
		/// <param name="sourceObject">An instance of System.TimeSpan.</param>
		/// <param name="builder">The StringBuilder to write the generated javascript code to.</param>
		public void RenderObjectInstantiation(object sourceObject, StringBuilder builder)
		{
			TimeSpan timeSpanObject = (TimeSpan)sourceObject;

			builder.Append(String.Format("new CsoGateway.System.TimeSpan(0,0,0,0,{0});", Convert.ToInt64(Math.Truncate(timeSpanObject.TotalMilliseconds)).ToString("D", CultureInfo.InvariantCulture)));
		}
	}
}
